package org.zlpt.contract.dao;

import com.owlike.genson.Genson;
import org.hyperledger.fabric.contract.Context;
import org.hyperledger.fabric.contract.ContractInterface;
import org.hyperledger.fabric.contract.annotation.Contract;
import org.hyperledger.fabric.contract.annotation.Transaction;
import org.hyperledger.fabric.shim.ChaincodeException;
import org.hyperledger.fabric.shim.ChaincodeStub;
import org.zlpt.entity.co.FapiaoCO;


@Contract(
        name = "fapiao"
)
public final class FapiaoContract implements ContractInterface {
    private final Genson genson = new Genson();

    @Transaction(intent = Transaction.TYPE.EVALUATE)
    public boolean FapiaoExists(final Context ctx, String fapiaoId){
        ChaincodeStub stub = ctx.getStub();
        FapiaoCO fapiao = genson.deserialize(fapiaoId, FapiaoCO.class);
        String fapiaoJSON = stub.getStringState(fapiao.formKey());

        return (fapiaoJSON != null && !fapiaoJSON.isEmpty());
    }

    @Transaction(intent = Transaction.TYPE.SUBMIT)
    public FapiaoCO CreateFapiao(final Context ctx, String fapiaoJson) {
        ChaincodeStub stub = ctx.getStub();
        if (FapiaoExists(ctx, fapiaoJson)) {
            String errorMessage = String.format("Fapiao already exists");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }
        FapiaoCO fapiao = genson.deserialize(fapiaoJson, FapiaoCO.class);
        stub.putStringState(fapiao.formKey(), fapiaoJson);
        return fapiao;
    }
    @Transaction(intent = Transaction.TYPE.EVALUATE)
    public FapiaoCO ReadFapiao(final Context ctx, String fapiaoId) {
        ChaincodeStub stub = ctx.getStub();
        String keyPrefiexFapiaoId = FapiaoCO.formKey(Long.valueOf(fapiaoId));
        String fapiaoJSON = stub.getStringState(keyPrefiexFapiaoId);
        if (fapiaoJSON == null || fapiaoJSON.isEmpty()) {
            String errorMessage = String.format("Fapiao does not exist");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }

        FapiaoCO fapiao = genson.deserialize(fapiaoJSON, FapiaoCO.class);
        return fapiao;
    }
    @Transaction(intent = Transaction.TYPE.SUBMIT)
    public FapiaoCO UpdateFapiao(final Context ctx, String fapiaoJson) {
        ChaincodeStub stub = ctx.getStub();
        FapiaoCO fapiao = genson.deserialize(fapiaoJson, FapiaoCO.class);

        if (!FapiaoExists(ctx, fapiaoJson)) {
            String errorMessage = String.format("Fapiao does not exist");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }
        String oldFapiaoJson = stub.getStringState(fapiao.formKey());
        FapiaoCO oldFapiao = genson.deserialize(oldFapiaoJson, FapiaoCO.class);
        String sortedJson = genson.serialize(fapiao);
        stub.putStringState(fapiao.formKey(), sortedJson);
        return oldFapiao;
    }
    @Transaction(intent = Transaction.TYPE.SUBMIT)
    public void DeleteFapiao(final Context ctx, String fapiaoId) {
        ChaincodeStub stub = ctx.getStub();

        if (!FapiaoExists(ctx, fapiaoId)) {
            String errorMessage = String.format("Fapiao does not exist");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }

        stub.delState(fapiaoId);
    }
}





